
jQuery(function() {

    $("#datepicker").datepicker({
        dateFormat: 'dd-mm-yy'
    });


    tinyMCE.init({
        mode: "textareas",
        theme: "advanced",
        plugins: "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,ibrowser",
        // theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
        // theme_advanced_buttons2 : ",",
        // theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,insertdate,inserttime,preview,|,ibrowser",
        theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,formatselect,fontselect,fontsizeselect,|,bullist,numlist,|,undo,redo,|,forecolor,backcolor,|",
        theme_advanced_buttons2: "cut,copy,paste,pastetext,pasteword,|,link,unlink,anchor,code,|,insertdate,inserttime,preview,|,ibrowser",
        // theme_advanced_buttons4 : "sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        // theme_advanced_buttons5 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
        theme_advanced_toolbar_location: "top",
        theme_advanced_toolbar_align: "left",
        theme_advanced_statusbar_location: "bottom",
        theme_advanced_resizing: true,
        theme_advanced_buttons3_add: "preview",
        plugin_preview_width: "500",
        plugin_preview_height: "600"
    });

    $('.icon-2').click(function() {
        var id = $(this).attr('id');
        var del = 'dell';

        if (confirm("Ти дійсно хочеш видалити пункт меню?")) {

            $.ajax({
                url: "/admin/ajaxfile/del_menu",
                type: 'post',
                dataType: 'json',
                data: {
                    id_menu: id,
                    del: del
                },
                success: function(data) {
                    if (data.data) {
                        $('#product-table #tr_' + id).remove();
                    }
                    else {
                        alert('Збій на сервері, обнови сторінку');
                    }

                }
            });

        } else {
            alert("Ти оприділись ))) ")
        }

    });


    /*загрузка прайсу*/
    $('#price_file').change(function() {

        $('#price_file_form').ajaxForm({
            type: 'post',
            dataType: 'json',
            success: upload_price
        });

        $('#price_file_form').submit();

    });
    function upload_price(data) {
        //alert(data.data);
        // alert(data.error);
        // $('#preview_file').html('<a href="/files/price/'+data.data+'">'+data.data+'</a>');

        if (data.error.length > 1) {
            alert(data.error);
        }
        else {
            $('#preview_file').html('<a href="/files/price/' + data.data + '">' + data.data + '</a>');
        }
    }
    /*------загрузка прайсу*/







    /*photo slider*/

    $('#add_photo_button').on('click', function() {
        $.ajax({
            type: "POST",
            cache: false,
            url: "/admin/slider_photo/ajax_popup_slider",
            data: {'ddd': 55},
            success: function(data) {
                $.fancybox(data);

            }
        });
        return false;
    });



    $('#add_photo_form input[type="file"]').live('change', function() {
        //alert($(this).parent().parent().siblings('#photo_slider').html());
        //return false;
        $('#add_photo_form').ajaxForm({
            dataType: 'json',
            success: function(data) {
                if (data.error.length > 1) {
                    alert(data.error);
                }
                else {
                    $('#add_photo_form input[name="temp_file"]').val(data.data);
                    $('#photo_slider').html('<img	 src="/files/temp/' + data.data + '">');
                }
            }
        });

        $('#add_photo_form').submit();
        /*обработка формы отправки файла*/

    });


    $('#submit_save_add_photo_slider').live('click', function() {
        if ($('#add_photo_form input[name="temp_file"]').val().length > 1) {
            var foto = $('#add_photo_form input[name="temp_file"]').val();
            var sorts = $('#add_photo_form input[name="sort"]').val();
            $.ajax({
                type: "POST",
                dataType: 'json',
                cache: false,
                url: "/admin/ajaxfile/save_add_photo_slider",
                data: {'foto': foto,
                    'sort': sorts},
                success: function(data) {
                    if (data.error.length > 1) {
                        alert(data.error);
                    }
                    else {
                        $('#list_slider_photo').append('<div class="one_photo_slider_block" ><img src="/files/foto_slider/' + data.foto + '" /><div class="del_edit_slider_photo"><a href="" class="del" alt="' + data.last_id + '">Видалити</a></div></div>');
                        $.fancybox.close();
                    }

                }
            });
        } else {
            alert('Потрібно загрузити фото');
        }

        return false;
    });

    $('#list_slider_photo .del').live('click', function() {
        if (confirm("Ти дійсно хочеш видалити фото?")) {
            var id = $(this).attr('alt');
            var del_block = this;
            $.ajax({
                type: "POST",
                dataType: 'json',
                cache: false,
                url: "/admin/ajaxfile/del_slider_photo",
                data: {'id': id},
                success: function(data) {
                    if (data.error.length > 1) {
                        alert(data.error);
                    }
                    else {
                        $(del_block).parent().parent().remove();
                    }

                }
            });
        } else {
            alert('Ти оприділись ))) ');
        }
        return false;
    });

    /*end photo slider*/


});








